package guet.dream.question;

import guet.dream.answer.Answer;
import guet.dream.db.DatabaseUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class QuestionDaoImpl implements QuestionDao {

	public Question findById(int id) {
		Connection con = DatabaseUtils.getConnection();
		String queryString = "select * from questions where id = ? ";
		try {
			PreparedStatement pstmt = con.prepareStatement(queryString);
			
			pstmt.setInt(1, id);
			
			ResultSet  rs = pstmt.executeQuery();
			while(rs.next()){
				String title = rs.getString("title");
				String content = rs.getString("content");
				 Timestamp time = rs.getTimestamp("create_time");
				 int viewCount = rs.getInt("view_count");
				 int answerCount = rs.getInt("answer_count");
				 
				 Question question = new Question(id,title,content);
				 			question.setCreateTime(time);
				 			question.setViewCount(viewCount);
				 			question.setAnswerCount(answerCount);
				 return question;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	public int save(Question question) {
		String queryString = "insert into questions(title,content,create_time) values(?,?,?)";
		
		Connection con = DatabaseUtils.getConnection();
		try {
			PreparedStatement pstmt = con.prepareStatement(queryString);
			pstmt.setString(1, question.getTitle());
			pstmt.setString(2, question.getContent());
			pstmt.setTimestamp(3, question.getCreateTime());
				
			boolean flag = pstmt.execute();
			int count = pstmt.getUpdateCount();
			
			return count;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;

		
	}

	public List<Answer> findAnswersByQuestionId(int id) {
	    List<Answer> answers = new ArrayList<Answer>();
	    
		Connection con = DatabaseUtils.getConnection();
		String queryString = "select * from answers where id = ? ";
		try {
			PreparedStatement pstmt = con.prepareStatement(queryString);
			
			pstmt.setInt(1, id);
			
			ResultSet  rs = pstmt.executeQuery();
			while(rs.next()){
				String content = rs.getString("content");
				 Timestamp time = rs.getTimestamp("answer_time");				 
				 Answer answer = new Answer(id,content,time);
				 answers.add(answer);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return answers;
	}

	public void findAnswer(Question question){
		Connection con = DatabaseUtils.getConnection();
		int id = question.getId();
		String queryString = "select * from answers where id = ? ";
		try {
			PreparedStatement pstmt = con.prepareStatement(queryString);
			
			pstmt.setInt(1, id);
			
			ResultSet  rs = pstmt.executeQuery();
			while(rs.next()){
				String content = rs.getString("content");
				 Timestamp time = rs.getTimestamp("answer_time");				 
				 Answer answer = new Answer(id,content,time);
				 question.getAnswers().add(answer);			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Question> findAll() {
	    List<Question> questions = new ArrayList<Question>();
	    
		Connection con = DatabaseUtils.getConnection();
		String queryString = "select * from questions";
		try {
			PreparedStatement pstmt = con.prepareStatement(queryString);
			
			ResultSet  rs = pstmt.executeQuery();
			while(rs.next()){
				int id = rs.getInt("id");
				String title = rs.getString("title");
				String content = rs.getString("content");
				Timestamp time = rs.getTimestamp("create_time");				 
				 int viewCount = rs.getInt("view_count");
				 int answerCount = rs.getInt("answer_count");
				 
				 Question question = new Question(id,title,content);
				 			question.setCreateTime(time);
				 			question.setViewCount(viewCount);
				 			question.setAnswerCount(answerCount);
				questions.add(question);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return questions;
	}

	public int update(Question question) {
		String queryString = "update questions set title=?, content=? where id=? limit 1";
		
		Connection con = DatabaseUtils.getConnection();
		try {
			PreparedStatement pstmt = con.prepareStatement(queryString);
			pstmt.setString(1, question.getTitle());
			pstmt.setString(2, question.getContent());
			pstmt.setInt(3, question.getId());
				
			boolean flag = pstmt.execute();
			int count = pstmt.getUpdateCount();
			
			return count;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

}
